package Aug8;

import java.util.Iterator;
import java.util.LinkedList;

class Student
	{
	int roll;
	String name;
	
	Student(int r, String n)
		{
		roll=r;
		name=n;
		}
	
	int getRoll()
		{
		return this.roll;
		}
	
	String getName()
		{
		return this.name;
		}
	
	public String toString()
		{
		return "Roll: " + this.getRoll()+" Name: "+this.getName() + ";";
		}
	}

public class LinkedListDemo 
{

	public static void main(String[] args) 
	{
	LinkedList<Student> sList = new LinkedList<Student>();
	sList.add(new Student(100, "Hermaione"));
	sList.add(new Student(200, "Ron"));
	sList.add(new Student(300, "Harry"));
	System.out.println("This size of the list " + sList.size());
	Iterator i= sList.iterator();
	System.out.println("Student List Contains: ");
	while(i.hasNext())
		{
		System.out.print(i.next()+" ");
		}
	System.out.println("Adding new students at first position");
	sList.addFirst(new Student(400,"Draco"));
	System.out.println("Now the student list contains: ");
	i=sList.iterator();
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	System.out.println("Number of Student in the list: " + sList.size());
	System.out.println("Adding student at the end of the list");
	sList.addLast(new Student(500,"Charlie"));
	System.out.println("Now the students in the list are: ");
	i=sList.iterator();
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	System.out.println("Now the size of the student list is: " + sList.size());
	System.out.println("Adding data at 2nd location");
	sList.add(1, new Student(600, "Rony"));
	System.out.println("Now the elements in the list are :");
	i=sList.iterator();
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	System.out.println("First Data: "+sList.getFirst());
	System.out.println("Last Data: "+sList.getLast());
	System.out.println("Class"+sList.getClass());
	System.out.println("Student at position 3: "+sList.get(2));
	Student s=sList.removeFirst();
	System.out.println("Student Removed: "+s);
	i=sList.iterator();
	System.out.println("Now the list contains: ");
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	Student l = sList.removeLast();
	System.out.println("The removed student is: "+l);
	i=sList.iterator();
	System.out.println("Now the list contains: ");
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	System.out.println("Removing the element at position 2");
	Student s2=sList.remove(1);
	System.out.println("The removed student is: "+s2);
	i=sList.iterator();
	System.out.println("Now the list contains: ");
	while(i.hasNext())
		{
		System.out.println(i.next());
		}
	}
}
